import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:get/get.dart';
import 'package:juming_test/model/home/home_kingkong_model.dart';
import 'package:juming_test/model/home/kingkong_management_section_model.dart';
import 'package:juming_test/module/home/kingkong_management/kingkong_management_logic.dart';
import 'package:juming_test/module/home/kingkong_management/widgets/kingkong_management_active_section.dart';
import 'package:juming_test/module/home/kingkong_management/widgets/kingkong_management_inactive_sections.dart';

@RoutePage()
class HomeKingkongManagementScreen extends StatefulWidget {
  const HomeKingkongManagementScreen({super.key});

  @override
  State<HomeKingkongManagementScreen> createState() =>
      _HomeKingkongManagementScreenState();
}

class _HomeKingkongManagementScreenState
    extends State<HomeKingkongManagementScreen> {
  final HomeKingkongManagementLogic _kingkongLogic = Get.put(
    HomeKingkongManagementLogic(),
  );

  @override
  void dispose() {
    super.dispose();
    Get.delete<HomeKingkongManagementLogic>();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Obx(() {
          if (_kingkongLogic.isEditing.value) {
            return Text('管理我的应用');
          } else {
            return Text('全部');
          }
        }),
        leading: Obx(() {
          if (_kingkongLogic.isEditing.value) {
            return IconButton(
              onPressed: () {
                _kingkongLogic.cancelEdit();
              },
              icon: Icon(Icons.close),
            );
          } else {
            return IconButton(
              onPressed: () {
                context.pop();
              },
              icon: Icon(Icons.arrow_back_ios),
            );
          }
        }),
      ),
      body: CustomScrollView(
        slivers: [
          // 已激活区域
          Obx(() {
            final List<HomeKingkongModel> list = [];
            if (_kingkongLogic.isEditing.value) {
              list.addAll(
                _kingkongLogic
                        .homeKingkongViewModel
                        .editSections
                        .value
                        .isNotEmpty
                    ? _kingkongLogic
                        .homeKingkongViewModel
                        .editSections
                        .value
                        .first
                        .items
                    : [],
              );
            } else {
              list.addAll(
                _kingkongLogic
                        .homeKingkongViewModel
                        .normalSections
                        .value
                        .isNotEmpty
                    ? _kingkongLogic
                        .homeKingkongViewModel
                        .normalSections
                        .value
                        .first
                        .items
                    : [],
              );
            }
            return KingkongManagementActiveSection(
              kingkongList: list,
              isEditing: _kingkongLogic.isEditing.value,
              onResetToDefault: () {
                _kingkongLogic.resetToDefault();
              },
              onEnterEdit: () {
                _kingkongLogic.enterEdit();
              },
              onConfirmEdit: () {
                _kingkongLogic.confirmEdit();
              },
              onDragSort: (List<HomeKingkongModel> list) {
                _kingkongLogic.dragSort(list);
              },
              onRemove: (HomeKingkongModel kingkong) {
                _kingkongLogic.homeKingkongViewModel.removeFromActive(kingkong);
              },
            );
          }),
          // 未激活模块区域
          Obx(() {
            final List<KingkongManagementSectionModel> list = [];
            if (_kingkongLogic.isEditing.value) {
              list.addAll(
                _kingkongLogic
                        .homeKingkongViewModel
                        .editSections
                        .value
                        .isNotEmpty
                    ? _kingkongLogic.homeKingkongViewModel.editSections.value
                        .sublist(1)
                    : [],
              );
            } else {
              list.addAll(
                _kingkongLogic
                        .homeKingkongViewModel
                        .normalSections
                        .value
                        .isNotEmpty
                    ? _kingkongLogic.homeKingkongViewModel.normalSections.value
                        .sublist(1)
                    : [],
              );
            }
            return KingkongManagementInactiveSections(
              kingkongSections: list,
              isEditing: _kingkongLogic.isEditing.value,
              onAdd: (HomeKingkongModel kingkong) {
                _kingkongLogic.homeKingkongViewModel.addToActive(kingkong);
              },
            );
          }),
        ],
      ),
    );
  }
}
